/**
 * 
 */
package com.googlecode.pcapj;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.bridj.Pointer;

import com.googlecode.pcapj.bridj.sockaddr;
import com.googlecode.pcapj.bridj.sockaddr_in6;

/**
 * 
 */
class IPv6AddressImpl extends SocketAddressImpl implements IPv6Address{
	private int portNumber;
	private Inet6Address flowInformation;
	private int scopeID; 
	
	/**
	 * @param sockaddr
	 */
	public IPv6AddressImpl(Pointer<sockaddr> sockaddr) {
		super(sockaddr);
		if(getSocketFamily() != SocketFamily.AF_INET6){
			throw new IllegalArgumentException("unexpected socket family : " + getSocketFamily());
		}
		Pointer<sockaddr_in6> pointer = sockaddr.as(sockaddr_in6.class);
		sockaddr_in6 sockaddr_in6 = pointer.get();
		portNumber = sockaddr_in6.sin6_port() & 0xffff;
		scopeID = sockaddr_in6.sin6_scope_id();
		try {
			setInetAddress(InetAddress.getByAddress(sockaddr_in6.sin6_addr().getBytes()));
			flowInformation = (Inet6Address)InetAddress.getByAddress(sockaddr_in6.sin6_flowinfo().getBytes());

		} catch (UnknownHostException e) {
			throw new PcapJRuntimeException(e);
			
		}
	}

	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.IPv6Address#getPortNumber()
	 */
	public int getPortNumber() {
		return portNumber;
	}

	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.IPv6Address#getFlowInformation()
	 */
	public Inet6Address getFlowInformation() {
		return flowInformation;
	}

	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.IPv6Address#getScopeID()
	 */
	public int getScopeID() {
		return scopeID;
	}
	@Override
	public String toString() {
		return getClass().getSimpleName() + "[socketFamily=" + getSocketFamily()
				+ ", portNumber=" + getPortNumber()
				+ ", scopeID=" + getScopeID()
				+ ", inetAddress=" + getInetAddress()
				+ ", flowInformation=" + getFlowInformation() + "]";
	}
}
